﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BF.Model;
using BF.IBLL.IXZCF_WS_ZJBCJDSBLLLibrary;
using Base.Core.UnityFactory;

namespace BF.Web.pages.XZCF_WS_ZJBCJDSPage
{
    public partial class Edit : BasePage
    {
        private readonly IXZCF_WS_ZJBCJDSBLL ibll = IOCFactory.GetIOCResolve<IXZCF_WS_ZJBCJDSBLL>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindContrl();
                if (!IsAdd)
                {
                    XZCF_WS_ZJBCJDS model = ibll.GetModel(P => P.gSLLJKey == Gkey);
                    if (model == null)
                        throw new NullReferenceException();
                    SetFormValue(model);
                    FCKeditor1.Value = model.sWPQD;
                }
                //查看页面
                if (!String.IsNullOrEmpty(Request.QueryString["State"]) && (Request.QueryString["State"] == "View"))
                {
                    btnAdd.Visible = false;
                    btnSave.Visible = false;
                    EnabledControls(this.Form);
                }
            }
        }

        private void BindContrl()
        {
            InitDLL(iBCFS, "sBCFS");
        }

        //带请选择的 下拉列表
        private void InitDLL(DropDownList ddl, string strCode)
        {
            List<dic_Dict> modelList = GetDictList(strCode);
            ddl.AppendDataBoundItems = true;
            ddl.Items.Add(new ListItem("--请选择--", ""));
            for (int i = 0; i < modelList.Count; i++)
            {
                ddl.Items.Add(new ListItem(modelList[i].sName.ToString(), modelList[i].sCode.ToString()));
            }
        }

        //TODO 获取页面上的值
        private XZCF_WS_ZJBCJDS GetFormValue(XZCF_WS_ZJBCJDS model)
        {
            model = (XZCF_WS_ZJBCJDS)WebDataControls.WebUIHelper.GetControlsModel(model, "XZCF_WS_ZJBCJDS", this.Form);
            return model;
        }

        private void SetFormValue(XZCF_WS_ZJBCJDS model)
        {
            WebDataControls.WebUIHelper.SetControlsValue(model, "XZCF_WS_ZJBCJDS", this.Form);
        }

        private void Add()
        {
            XZCF_WS_ZJBCJDS model = new XZCF_WS_ZJBCJDS() { gKey = Guid.NewGuid() };
            GetFormValue(model);
            model.gSLLJKey = Gkey;
            model.sWPQD = FCKeditor1.Value;
            if (ibll.Add(model))
            {
                MessageBox("新增成功！", "function(e){UpatePage('" + model.gSLLJKey.ToString() + "');}");
            }
            else
            {
                Messages("新增失败！");
            }
        }

        private void Update()
        {
            XZCF_WS_ZJBCJDS model = ibll.GetModel(P => P.gSLLJKey == Gkey);
            GetFormValue(model);
            model.sWPQD = FCKeditor1.Value;
            if (ibll.Update())//修改操作
            {
                Messages("修改成功！", this);
            }
            else
            {
                Messages("修改失败！");
            }
        }

        //TODO 新增或者修改事件
        protected void btnSave_Click(object sender, EventArgs e)
        {
            //新增
            if (IsAdd)
            {
                Add();
            }
            else //编辑
            {
                Update();
            }
        }

        private Guid Gkey
        {
            get
            {
                if (!string.IsNullOrEmpty(Request.QueryString["sID"]))
                {
                    string skey = Request.QueryString["sID"];
                    Guid key = default(Guid);
                    Guid.TryParse(skey, out key);
                    return key;
                }
                else
                {
                    return default(Guid);
                }
            }
        }

        private bool IsAdd
        {
            get { XZCF_WS_ZJBCJDS model = ibll.GetModel(P => P.gSLLJKey == Gkey); return model == null; }
        }
    }
}

